package edu.rit.drh4818.raytracing.object;

import edu.rit.drh4818.raytracing.util.Intersection;
import edu.rit.drh4818.raytracing.util.Ray;


/**
 * A plane unlimited in z dimension. Limited to changing in only x dimension.
 * 
 * @author Darren Headrick
 *
 */
public class Plane extends SceneObject
{
	/**
	 * Coordinates of plane.
	 */
	private int xmin, xmax;
	
	/**
	 * Y coordinate for plane.
	 */
	private int y;
	
	/**
	 * 
	 * @param xmin
	 * @param xmax
	 * @param y
	 * @param prop
	 */
	public Plane( int xmin, int xmax, int y, ObjectProperties prop )
	{
		super( prop );
		this.xmin = xmin; this.xmax = xmax; this.y = y;
	}
	
	/*
	 * Check for intersection.
	 */
	public Intersection getIntersection(Ray ray) 
	{
		double t = (y - ray.y0)/ray.yd;
		int x = (int)ray.getX( t );
		
		if( t <= 0 || x < xmin || x > xmax )
		{
			return Intersection.NONE;
		}
		
		return new Intersection( t, this, ray );
	}

	/*
	 * The normal is always straight up or down in y direction, 
	 *  depending on direction of rays travel.
	 */
	public Ray getNormal(Intersection in) 
	{
		double ix = in.getX(), iy = in.getY(), iz = in.getZ();
		return new Ray( ix, iy, iz, 0,(in.ray.yd>0)?-1:1,0  );
	}
}
